﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;

namespace vCrossPost
{
    public partial class FrmOptions : Form
    {
        private readonly Converter<string, IDictionary<string, string>> _servicesRetriever;
        private readonly Predicate<string> _userKeyValidator;

        /// <summary>
        /// Gets or sets the user key.
        /// </summary>
        /// <value>The user key.</value>
        public string UserKey
        {
            get { return txtKey.Text; }
            set
            {
                txtKey.Text = value;
                bwServices.RunWorkerAsync(value);
            }
        }

        /// <summary>
        /// Gets or sets the service.
        /// </summary>
        /// <value>The service.</value>
        public string Service { get; set; }

        public FrmOptions(Predicate<string> userKeyValidator,
            Converter<string, IDictionary<string, string>> servicesRetriever)
        {
            if (userKeyValidator == null) throw new ArgumentNullException("userKeyValidator");
            if (servicesRetriever == null) throw new ArgumentNullException("servicesRetriever");

            _userKeyValidator = userKeyValidator;
            _servicesRetriever = servicesRetriever;

            InitializeComponent();
        }

        private void bwServices_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = _servicesRetriever((string)e.Argument);
        }

        private void bwServices_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var selectedService = cbbServices.SelectedValue as string;
            if (string.IsNullOrEmpty(selectedService))
                selectedService = Service;

            cbbServices.DataSource = null;
            cbbServices.ValueMember = "Key";
            cbbServices.DisplayMember = "Value";

            cbbServices.DataSource = new BindingSource(e.Result, null);
            cbbServices.SelectedValue = selectedService;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            Service = cbbServices.SelectedValue as string;
            DialogResult = DialogResult.OK;
        }

        private void cmdUserKey_Click(object sender, EventArgs e)
        {
            using (var frmUserKey = new FrmUserKey(_userKeyValidator))
            {
                if (frmUserKey.ShowDialog(this) == DialogResult.OK)
                {
                    txtKey.Text = frmUserKey.UserKey;
                    bwServices.RunWorkerAsync(frmUserKey.UserKey);
                }
            }
        }

        private void lnkKey_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start("http://ping.fm/key");
            }
            catch
            {
                MessageBox.Show(this, Properties.Resources.ManualKeyRetrieve,
                    Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}